# 备注：进程之间的内存空间是隔离的。

# 开进程的方法一:
"""
import time
import random
from multiprocessing import Process


def piao(name):
    print('%s 子线程 doing' % name)
    time.sleep(random.randrange(1, 5))
    print('%s 子线程 end' % name)


if __name__ == '__main__':
    p1 = Process(target=piao, args=('001',))  # 必须加,号
    p2 = Process(target=piao, args=('002',))
    p3 = Process(target=piao, args=('003',))
    p4 = Process(target=piao, args=('004',))

    p1.start()
    p2.start()
    p3.start()
    p4.start()
    print('主线程')
"""

# 开进程的方法二:
"""
import time
import random
from multiprocessing import Process


class Piao(Process):
    def __init__(self, name):
        super().__init__()
        self.name = name

    def run(self):
        print('%s 子线程 doing' % self.name)

        time.sleep(random.randrange(1, 5))
        print('%s 子线程 end' % self.name)


if __name__ == '__main__':
    p1 = Piao('001')  # 必须加,号
    p2 = Piao('002')
    p3 = Piao('003')
    p4 = Piao('004')

    p1.start()
    p2.start()
    p3.start()
    p4.start()
    print('主线程')
"""

# process 的 join() 方法
# join：主进程等待子进程结束
# 让主线程等待p的结束，卡住的是主线程而绝非进程p
from multiprocessing import Process
import time
import random


class Piao(Process):
    def __init__(self, name):
        self.name = name
        super().__init__()

    def run(self):
        print('%s is piaoing' % self.name)
        time.sleep(random.randrange(5, 6))
        print('%s is piao end' % self.name)


if __name__ == '__main__':
    p = Piao('子线程')
    p.start()
    p.join(2)  # 等待 p 停止,等 5 秒就不再等了
    print('开始')
    for i in range(10):
        print(i)
